package com.gitee.sop.layui.adminserver.module.background.admin.rest;

import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.dto.SysApplicationDTO;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysApplication;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysApplicationService;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import com.gitee.sop.layui.adminserver.module.base.util.SqlTreeUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.gitee.sop.adminserver.modules.background.admin.entity.SysApplication模型http接口处理
 *
 * @author: admin
 * @date: 2020-04-24
 * @version: 1.0.0
 */
@RestController
@RequestMapping(value = "/admin/sysApplication", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SysApplicationRestController {

    @Lazy
    @Resource
    private SysApplicationService baseService;

    @PostMapping(value = {"/add"})
    public Result add(SysApplicationDTO query, String parentAppId, String templateAppId) {
        SysApplication sysApplication = ObjectUtil.switchAllFieldValue(query, SysApplication.class);
        baseService.add(sysApplication, parentAppId, templateAppId);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"})
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        baseService.removeById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"})
    public Result updateById(@PathVariable(value = "id") Integer id, SysApplicationDTO query) {
        SysApplication sysApplication = new SysApplication();
        ObjectUtil.switchAllFieldValue(sysApplication, query);
        baseService.updateById(sysApplication.setId(id));
        return Result.success();
    }

    @RequestMapping(value = {"/assign/{userUuid}"})
    public Result assign(@PathVariable("userUuid") String userUuid, String appId) {
        baseService.assign(userUuid, appId);
        return Result.success();
    }

    @RequestMapping(value = {"/cancel/{userUuid}"})
    public Result cancel(@PathVariable("userUuid") String userUuid, String appId) {
        baseService.cancel(userUuid, appId);
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"})
    public Result searchAll() {
        return Result.success(baseService.getChildListForCurrentApplication());
    }

    @RequestMapping(value = {"/search/available"})
    public Result searchAvailable() {
        List<Map<String, Object>> list = ServiceContext.me().desensitizationAllApplicationAndGet();
        return Result.success(SqlTreeUtil.tree(ServiceContext.me().desensitizationAllApplicationAndGet().stream().collect(ArrayList::new
                , (res, ele) -> {
                    res.add(ObjectUtil.toBean(ObjectUtil.toJson(ele), SysApplication.class));
                }, (l, r) -> {
                })));
    }

    @RequestMapping(value = {"/search/self"})
    public Result searchSelf(String userUuid) {
        SysApplication cur = ServiceContext.me().getCurrentApplication();
        List<SysApplication> list = baseService.getListByCurrentUserCreated();
        //添加分配标识
        List<SysApplication> relationList = ObjectUtil.isEmpty(userUuid) ? ServiceContext.me().getRelationApplication() : baseService.getRelationListByUserUuid(userUuid);

        List<Map<String, Object>> treeList = SqlTreeUtil.tree(list);
        //建立树形结构，即第一层级应用的pid必须为0
        if (!treeList.isEmpty()) {
            treeList.forEach(map -> {
                int pid = Integer.parseInt(String.valueOf(map.get("pid")));
                if (pid == cur.getId().intValue()) {
                    map.put("pid", 0);
                }
            });
        }

        List<Map<String, Object>> result = treeList.stream().collect(ArrayList::new, (res, ele) -> {
            Map<String, Object> map = ObjectUtil.toMap(ele);
            res.add(map);

            for (int i = 0; i < relationList.size(); ++i) {
                if (ele.get("appId").equals(relationList.get(i).getAppId())) {
                    map.put("assignFlag", true);
                    break;
                }
            }
        }, (l, r) -> {
        });

        return Result.success(result);
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") int id) {
        return Result.success(baseService.getById(id));
    }
}
